package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.domain.Price;
import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.service.PriceService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.ShiroUtils;
import com.wsd.smartcarwasher.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Api(tags = "(新增)价格套餐")
@RestController
@CrossOrigin
public class PriceController {

    @Resource
    private PriceService PriceService;
    @Resource
    private WxService wxService;


    @ApiOperation(
            value = "小程序-获取当前有效的价格套餐 ",
            notes = "获取当前有效的价格套餐",
            produces = "application/json",
            response = Price.class
    )

    @RequestMapping(
            value = {"/v1/getPrice"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getPrice() {
        //String sessionId = request.getHeader("authorization");

        return  ReturnUtil.Success("获取价格套餐成功",this.PriceService.getPrice());
    }

    @RequestMapping(
            value = {"/v1/getMachinePrice"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ApiOperation(notes = "设备Id", value = "machineId")
    @ResponseBody
    public ModelMap getMachinePrice(@RequestParam("machineId") int machineId, HttpServletRequest request) {
        //String sessionId = request.getHeader("authorization");
        BigDecimal balance=BigDecimal.ZERO;
        if(!StringUtil.isNullOrEmpty(request.getHeader("authorization")))
        {
            String sessionId = request.getHeader("authorization");
            UserInfo user = this.wxService.getUserInfoBySessionId(sessionId);
            balance=StringUtil.isNullOrEmpty(user.getBalance())?BigDecimal.ZERO:user.getBalance();
        }

        return  this.PriceService.getMachinePrice(machineId)==null?
                ReturnUtil.Error("该机器没有设置对应价格"):
                ReturnUtil.Success("获取对应机器价格套餐成功",this.PriceService.getMachinePrice(machineId)).addAttribute("balance",balance);
    }






    @ApiOperation(
            value = "获取当前有效的价格套餐 ",
            notes = "获取当前有效的价格套餐",
            produces = "application/json",
            response = Price.class
    )

    @RequestMapping(
            value = {"/v1/sys/getPrice"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getAlPrice() {
        //String sessionId = request.getHeader("authorization");

        return  ReturnUtil.Success("获取价格套餐成功",this.PriceService.getAlPrice());
    }

    @Log("新增了价格套餐")
    @ApiOperation(
            value = "新增价格套餐  ",
            notes = "新增价格套餐",
            produces = "application/json",
            response = Price.class
    )

    @RequestMapping(
            value = {"/v1/sys/priceAdd"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap priceAdd(@RequestBody Price price) {
        //String sessionId = request.getHeader("authorization");
        if(null == price || null ==price.getType()){
            return  ReturnUtil.Error("无效参数");
        }
        price.setUserid(ShiroUtils.getUser().getId());
        int rows =this.PriceService.priceAdd(price);
        if(rows<1){
            return  ReturnUtil.Error("失败");
        }
        return  ReturnUtil.Success("成功");

    }

    @Log("修改了价格套餐")
    @ApiOperation(
            value = "修改价格套餐 ",
            notes = "修改价格套餐",
            produces = "application/json",
            response = Price.class
    )

    @RequestMapping(
            value = {"/v1/sys/priceUpdate"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap priceUpdate(@RequestBody Price price) {
        //String sessionId = request.getHeader("authorization");
        price.setUserid(ShiroUtils.getUser().getId());
        int rows =this.PriceService.priceUpdate(price);
        if(rows>0){
            return  ReturnUtil.Success("成功");
        }
        return  ReturnUtil.Error("失败");
    }

    @Log("删除了价格套餐")
    @ApiOperation(
            value = "删除价格套餐 ",
            notes = "删除价格套餐",
            produces = "application/json",
            response = Price.class
    )
    @RequestMapping(
            value = {"/v1/sys/deletePrice"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap deletePrice(@RequestParam Integer id) {
        int rows =this.PriceService.deletePrice(id);
        if(rows>0){
            return  ReturnUtil.Success("成功");
        }
        return  ReturnUtil.Error("失败");
    }

}
